package com.rongzhe.house.manager;

import android.text.TextUtils;

import com.rongzhe.house.entity.ResponseEntityVo;
import com.rongzhe.house.entity.voo.Config;
import com.rongzhe.house.internet.InternetRequestWorker;
import com.rongzhe.house.internet.ResponseDispatcher;
import com.rongzhe.house.internet.net.ConfigInterface;

import java.util.List;

/**
 * Created by huang on 2018/3/10.
 */

public class ConfigManager {
    private ConfigManager() {
    }
    private static ConfigManager sInstance;
    public static ConfigManager getInstance() {
        if (sInstance == null) {
            sInstance = new ConfigManager();
        }
        return sInstance;
    }
    private List<Config> mLoadedSystemConfig = null;

    public String getAboutUsUrl() {
        return getValue("about_me_h5");
    }

    public String getServiceTel() {
        return getValue("online_delegation_tel");
    }

    public String getContractUrl() {
        return getValue("rent_contract_h5");
    }

    public String getRentContract() {
        return getValue("rent_contract_h5");
    }

    public String getEntrustPage() {
        return getValue("entrust_h5");
    }

    public String getRenContractText() {
        return getValue("rent_contract_txt");
    }

    public String getServiceLifePage() {
        return getValue("service_life_h5");
    }

    public String getValue(String lable) {
        if (TextUtils.isEmpty(lable) || mLoadedSystemConfig == null) {
            return null;
        }
        for (Config config : mLoadedSystemConfig) {
            if (lable.equals(config.getLabel())) {
                return config.getValue();
            }
        }
        return null;
    }

    public void loadSystemConfig() {
        InternetRequestWorker.getInstance().asyncNetwork(
                InternetRequestWorker.getInstance().create(ConfigInterface.class).getSystemConfig(),
                new ResponseDispatcher(null) {
                    @Override
                    protected void handleSuccess(ResponseEntityVo data) {
                        if(data != null && data.success()) {
                            List<Config> configs = (List<Config>) data.getData();
                            mLoadedSystemConfig = configs;
                        }
                    }
                });
    }
}
